package com.zzk.lanouhn.myapplication1.adapters;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.activity.AuthorCollectSingleGoodActivity;
import com.zzk.lanouhn.myapplication1.modles.CollectArticlesDetails;

import java.util.ArrayList;
import java.util.List;

/**
 * 收藏->文章 适配器
 * Created by lanouhn on 16/9/18.
 */
public class CollectArticlesDetailsAdapter extends RecyclerView.Adapter<CollectArticlesDetailsAdapter.collectArticlesHolder> {

    private Context context;
    private List<CollectArticlesDetails.DataBean.ProductListBean> articlesContents = new ArrayList<>();

    public CollectArticlesDetailsAdapter(Context context, List<CollectArticlesDetails.DataBean.ProductListBean> articlesContents) {
        this.context = context;
        this.articlesContents = articlesContents;
    }

    @Override
    public collectArticlesHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_content_articles_collect, null);
        collectArticlesHolder holder = new collectArticlesHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(collectArticlesHolder holder, final int position) {
        holder.tv_title.setText(articlesContents.get(position).getTitle());
        holder.tv_content.setText(articlesContents.get(position).getDesc());
        holder.tv_goodName.setText(articlesContents.get(position).getTitle());
        holder.tv_goodPrice.setText("￥" + articlesContents.get(position).getPrice());
        holder.tv_goodLikes.setText(articlesContents.get(position).getLikes());
        if (!articlesContents.get(position).getPic().equals("")) {
            Picasso.with(context).load(articlesContents.get(position).getPic()).into(holder.iv_pic);
        } else {
            Picasso.with(context).load(articlesContents.get(position).getPics().get(0).getUrl()).into(holder.iv_pic);
        }
        Picasso.with(context).load(articlesContents.get(position).getThumbnail_pic()).into(holder.iv_goodPic);
        holder.rel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, AuthorCollectSingleGoodActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("authorSingle",articlesContents.get(position).getId());
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return articlesContents.size();
    }

    public class collectArticlesHolder extends RecyclerView.ViewHolder {
        private TextView tv_title, tv_content, tv_goodName, tv_goodPrice, tv_goodLikes;
        private ImageView iv_pic, iv_goodPic;
        private RelativeLayout rel;

        public collectArticlesHolder(View itemView) {
            super(itemView);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title_item_collect);
            tv_content = (TextView) itemView.findViewById(R.id.tv_content_item_collect);
            tv_goodName = (TextView) itemView.findViewById(R.id.tv_item_name_details_collect);
            tv_goodPrice = (TextView) itemView.findViewById(R.id.tv_item_price_details_collect);
            tv_goodLikes = (TextView) itemView.findViewById(R.id.tv_like_counts_details_collect);
            iv_pic = (ImageView) itemView.findViewById(R.id.iv_picOne_item_collect);
            iv_goodPic = (ImageView) itemView.findViewById(R.id.iv_item_pic_details_collect);
            rel= (RelativeLayout) itemView.findViewById(R.id.rl_to_single_shop);
        }
    }
}
